use std::collections::HashMap;
#[derive(Debug, PartialEq)]
pub enum Method {
    Get,
    Post,
    Uninitialized,
}
impl From<&str> for Method {
    fn from(value: &str) -> Self {
        match value {
            "Get" => Method::Get,
            "Post" => Method::Post,
            _ => Method::Uninitialized,
        }
    }
}

#[derive(Debug, PartialEq)]
pub enum Version {
    V1_1,
    _V2_0,
    Uninitialized,
}
impl From<&str> for Version {
    fn from(value: &str) -> Self {
        match value {
            "HTTP/1.1" => Version::V1_1,
            _ => Version::Uninitialized,
        }
    }
}

#[derive(Debug, PartialEq)]
pub enum Resource {
    Path(String),
}
#[derive(Debug)]
pub struct HttpRequest {
    pub method: Method,
    pub version: Version,
    pub resource: Resource,
    pub headers: HashMap<String, String>,
    pub message_body: String,
}
impl From<String> for HttpRequest {
    fn from(value: String) -> Self {
        let mut parsed_method = Method::Uninitialized;
        let mut parsed_version = Version::Uninitialized;
        let mut parsed_resource = Resource::Path(" ".to_string());
        let mut parsed_headers = HashMap::new();
        let mut parsed_msg_body = "";
        for line in value.lines() {
            if line.contains("HTTP") {
                let (method, version, resource) = prcess_req_line(line);
                parsed_method = method;
                parsed_version = version;
                parsed_resource = resource;
            } else if line.contains(":") {
                let (key, value) = process_headers_line(line);
                parsed_headers.insert(key, value);
            } else if line.len() == 0 {
            } else {
                parsed_msg_body = line;
            }
        }
        HttpRequest {
            method: parsed_method,
            version: parsed_version,
            resource: parsed_resource,
            headers: parsed_headers,
            message_body: parsed_msg_body.to_string(),
        }
    }
}

fn prcess_req_line(_line: &str) -> (Method, Version, Resource) {
    let mut str = _line.split_whitespace();
    let method = str.next().unwrap();
    let resource = str.next().unwrap();
    let version = str.next().unwrap();
    (
        method.into(),
        version.into(),
        Resource::Path(resource.to_string()),
    )
}
fn process_headers_line(line: &str) -> (String, String) {
    let mut c = line.split(":");
    let mut key = String::new();
    let mut value = String::new();
    if let Some(x) = c.next() {
        key = x.to_string();
    }
    if let Some(y) = c.next() {
        value = y.to_string();
    }
    (key, value)
}
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn method_test() {
        let m: Method = "Get".into();
        assert_eq!(m, Method::Get)
    }
    #[test]
    fn version_test() {
        let v: Version = "HTTP/1.1".into();
        assert_eq!(v, Version::V1_1);
    }

    #[test]
    fn http_test() {
        let s = String::from("Get /greeting HTTP/1.1\r\nHost: localhost:3000\r\nUser-Agent: curl/7.71.1\r\nAccept: */*\r\n\r\n");
        let mut headers_expected = HashMap::new();
        headers_expected.insert("Host".into(), " localhost".into());
        headers_expected.insert("Accept".into(), " */*".into());
        headers_expected.insert("User-Agent".into(), " curl/7.71.1".into());
        let req: HttpRequest = s.into();
        assert_eq!(Method::Get, req.method);
        assert_eq!(Version::V1_1, req.version);
        assert_eq!(Resource::Path("/greeting".to_string()), req.resource);
        assert_eq!(headers_expected, req.headers);
    }
}
